package cn.fu.security.eigamal;

import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.spec.DHParameterSpec;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * @Title: MyEIGamal.java
 * @Package cn.fu.security.eigamal
 * @Description: 公钥加密算法 只有Bouncy Castle实现 只有私钥解密
 * @author zhangxiaofu
 * @Company www.viewhigh.com
 * @date 2016年8月21日 下午6:10:03
 * @version V1.0
 */
public class MyElGamal {

//	private static String src = "99fu";

	public static void main(String[] args) {
		bcEIGamal();

	}

	public static void bcEIGamal() {
		try {
			Security.addProvider(new BouncyCastleProvider());
			AlgorithmParameterGenerator algorithmParameterGenerator = AlgorithmParameterGenerator
					.getInstance("ElGamal");
			algorithmParameterGenerator.init(256);
			AlgorithmParameters algorithmParameters = algorithmParameterGenerator.generateParameters();
			DHParameterSpec dhParameterSpec = (DHParameterSpec) algorithmParameters
					.getParameterSpec(DHParameterSpec.class);
			KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ElGamal");
			keyPairGenerator.initialize(dhParameterSpec, new SecureRandom());
			KeyPair keyPair = keyPairGenerator.generateKeyPair();
			PublicKey publicKey = keyPair.getPublic();
			PrivateKey privateKey = keyPair.getPrivate();
			System.err.println("public key :" + Base64.encodeBase64String(publicKey.getEncoded()));
			System.err.println("private key :" + Base64.encodeBase64String(privateKey.getEncoded()));

			// 加密
			// 解密
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
